package 成绩管理系统;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginPage extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton teacherButton;
    private JButton studentButton;

    public LoginPage() {
        setTitle("学生成绩管理系统");
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 设置背景颜色
        getContentPane().setBackground(new Color(255, 255, 255));

        // 使用网格布局管理器
        setLayout(new GridLayout(5, 1, 10, 10));

        // 添加标题
        JLabel titleLabel = new JLabel("学生成绩管理系统");
        titleLabel.setFont(new Font("Arial", Font.BOLD, 18));
        titleLabel.setHorizontalAlignment(JLabel.CENTER);
        add(titleLabel);

        // 添加账号输入框和提示文字
        JPanel usernamePanel = new JPanel();
        JLabel usernameLabel = new JLabel("账号：");
        usernameField = new JTextField(20);
        usernamePanel.add(usernameLabel);
        usernamePanel.add(usernameField);
        add(usernamePanel);

        // 添加密码输入框和提示文字
        JPanel passwordPanel = new JPanel();
        JLabel passwordLabel = new JLabel("密码：");
        passwordField = new JPasswordField(20);
        passwordPanel.add(passwordLabel);
        passwordPanel.add(passwordField);
        add(passwordPanel);

        teacherButton = new JButton("教师登录");
        teacherButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                if (validateLogin(username, password, "teacher")) {
                    dispose();
                    new TeacherPage();
                } else {
                    JOptionPane.showMessageDialog(null, "账号或密码错误，请重新输入！", "登录失败", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        add(teacherButton);

        studentButton = new JButton("学生登录");
        studentButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                if (validateLogin(username, password, "student")) {
                    dispose();
                    new StudentPage();
                } else {
                    JOptionPane.showMessageDialog(null, "账号或密码错误，请重新输入！", "登录失败", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        add(studentButton);

        setVisible(true);
    }

    private boolean validateLogin(String username, String password, String userType) {
        // 预置两个账号用于登录验证
        String teacherUsername = "teacher";
        String teacherPassword = "123456";
        String studentUsername = "student";
        String studentPassword = "654321";

        if (userType.equals("teacher")) {
            if (username.equals(teacherUsername) && password.equals(teacherPassword)) {
                return true;
            }
        } else if (userType.equals("student")) {
            if (username.equals(studentUsername) && password.equals(studentPassword)) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        new LoginPage();
    }
}